﻿using ISSMoviePlex.DAL.Repositories;
using ISSMoviePlex.DAL.Models;
using System.Linq;

namespace ISSMoviePlex.BLL
{
    public class AccountBLL
    {
        public enum Role { Admin, Member, Default };

        private readonly AccountRepository _accRepository;
        private readonly MemberRepository _memberRepository;
        private readonly MemberStateRepository _memberStateRepository;

        public AccountBLL()
        {
            _accRepository = AccountRepository.Instance;
            _accRepository.Initialize();
            _memberRepository = MemberRepository.Instance;
            _memberRepository.Initialize();
            _memberStateRepository = MemberStateRepository.Instance;
            _memberStateRepository.Initialize();
        }

        public void Register(RegisterModel newUser)
        {
            int userId = _accRepository.Register(newUser);
            newUser.Member.MemberId = userId;
            newUser.Member.MemberStateId = _memberStateRepository.GetMemberStateByName("Standard").MemberStateId;
            newUser.Member.MoneySpent = 0;
            _memberRepository.Add(newUser.Member);
            _memberRepository.Save();
        }

        public bool Login(LoginModel loginUser, Role role, ref string errorMessage)
        {
            var user = _accRepository.GetUser(loginUser.UserName);
            if (user != null)
            {
                var userRole = user.webpages_Roles.ElementAtOrDefault(0);
                if (userRole != null && (role != Role.Default && userRole.RoleName != role.ToString()))
                {
                    errorMessage = "This account is not in role " + role;
                    return false;
                }
                if (user.Status)
                {
                    if (_accRepository.Login(loginUser))
                    {
                        return true;
                    }
                }
                else
                {
                    errorMessage = "This account has been blocked.";
                    return false;
                }
            }
            errorMessage = "The user name or password provided is incorrect.";
            return false;
        }

        public void Logout()
        {
            _accRepository.Logout();
        }

        public bool ChangePassword(string userName, LocalPasswordModel model)
        {
            return _accRepository.ChangePassword(userName, model);
        }

        public string GetUserName(int id)
        {
            return _accRepository.GetUser(id).UserName;
        }

        public void ChangeAccountStatus(int memberId)
        {
            UserProfile member = _accRepository.FindById(memberId);
            member.Status = !member.Status;
            _accRepository.Save();
        }
        public int GetCurrentUserId()
        {
            return _accRepository.GetCurrentUserId();
        }
    }
}
